/* 
 * MComboBoxEditor.java
 * 重写可编辑组合框的编辑器
 */
package org.vacoor.ui.qq.combobox;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
//import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.plaf.basic.BasicComboBoxEditor;

/* 
 * Combobox编辑器
 * @author Vacoor
 */
public class QQComboBoxEditor extends BasicComboBoxEditor {

    private QQComboBox MComboBox;
    private JPanel editorPanel;
    private JLabel labelIcon;   //编辑框中加入的图标
    private JTextField editorField;
    private Font editorFont;     //编辑框字体
    private Color editorBackColor;       //编辑框背景颜色
    private Dimension iconDim;      //图标标签的大小
    private Object objItem;         //编辑框中的内容项
    /*
     * 无选项时文本，在构造时从ComboBox得到
     * 构造后修改此参数需要调用updateNoItemText实现
     */
    private String noItemText;
    private Font noItemTextFont;        //无选项时候显示默认文本的字体
    private Color noItemEditorForeColor;        //显示默认文本的颜色
    private Border myComDefaultBorder;  //MComboBox的默认边框
    private Border myComRollBorder;     //鼠标进入边框

    public QQComboBoxEditor(QQComboBox comboBox) {
        this.MComboBox = comboBox;
        editorPanel = new JPanel(new BorderLayout());
        labelIcon = new JLabel();
        editorField = new JTextField("");
        editorFont = new Font(Font.DIALOG, Font.PLAIN, 14);
        editorBackColor = Color.white;
        iconDim = comboBox.getMinIconDim();
        //noItemText = "请输入账号";
        noItemText = MComboBox.getNoItemText();
        noItemTextFont = new Font("微软雅黑", Font.PLAIN, 12);
        noItemEditorForeColor = new Color(140, 140, 140);

        myComDefaultBorder = BorderFactory.createLineBorder(new Color(167, 195, 212));
        myComRollBorder = BorderFactory.createCompoundBorder(
                new javax.swing.border.LineBorder(new Color(96, 200, 253), 1, true),
                new javax.swing.border.LineBorder(new Color(51, 139, 192), 1, true));

        editorPanel.setBackground(editorBackColor);
        //加入空白边框 上左下右
        editorPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
        editorField.setBackground(editorBackColor);
        editorField.setBorder(null);
        editorField.setFont(editorFont);

        //设置无选项时的文本
        editorField.addFocusListener(new FocusListener() {

            @Override
            public void focusGained(FocusEvent e) {
                if (editorField.getText().equals(noItemText)) {
                    editorField.setFont(editorFont);
                    editorField.setForeground(Color.BLACK);
                    editorField.setText("");
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                if (editorField.getText().equals("") || editorField.getText() == null) {
                    editorField.setFont(noItemTextFont);
                    editorField.setForeground(noItemEditorForeColor);
                    editorField.setText(noItemText);
                }
            }
        });

        //改变MComboBox边框颜色
        editorField.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                MComboBox.setBorder(myComRollBorder);
            }

            @Override
            public void mouseExited(java.awt.event.MouseEvent evt) {
                MComboBox.setBorder(myComDefaultBorder);
            }
        });

        /*
         * 将图标加入面板，如果需要可以加入
         * 这个是在构造方法中实现
         * 构造完成后是否显示图标在updateIcon方法中实现
         */
        if (comboBox.getFieldIconShow()) {
            editorPanel.add(labelIcon, BorderLayout.WEST);
        }
        editorPanel.add(editorField, BorderLayout.CENTER);
    }
    //构造方法结束
    //updateIcon用于实现及时更新是否显示图标(主要为MComboBox中set方法)

    protected void updateIcon(boolean isShow) {
        if (isShow) {
            editorPanel.add(labelIcon, BorderLayout.WEST);
        } else {
            editorPanel.remove(labelIcon);
        }
        editorPanel.updateUI();
    }
    //updateNoItemText用于及时更新无线选项时文本框内容

    protected void updateNoItemText(String text) {
        //注意语句顺序,将影响if
        if (editorField.getText().equals(noItemText)) {
            editorField.setText(text);
        }
        this.noItemText = text;
    }

    //修改了EditorComponent需重写getEditorComponent方法
    @Override
    public Component getEditorComponent() {
        return editorPanel;
    }

    @Override
    public Object getItem() {
        return objItem;
    }

    //重写设置选择选项后编辑框响应方法
    @Override
    public void setItem(Object newValue) {
        objItem = newValue;
        if (newValue != null) {
            QQComItem item = (QQComItem) newValue;
            editorField.setText(item.getNum());
            //调用MComboBoxPopup.getResizePicture进行图片缩放
            labelIcon.setIcon(QQComboBoxPopup.getResizePicture((ImageIcon) item.getIcon(), iconDim));
        } else {
            editorField.setFont(noItemTextFont);
            editorField.setForeground(noItemEditorForeColor);
            editorField.setText(noItemText);
            labelIcon.setIcon(null);
        }
    }

    //重写监听器添加/删除方法
//    @Override
//    public void addActionListener(ActionListener l) {
//        editorField.addActionListener(l);
//    }
//
//    @Override
//    public void removeActionListener(ActionListener l) {
//        editorField.removeActionListener(l);
//    }
    @Override
    public void selectAll() {
        // Ignore  
    }
}